﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.Core.Actions
{
    public class VoBoAssignDocumentCommand : IBaseCommand
    {

        #region Fields

        SPFile _sourceFile;
        SPList _targetLibrary;
        int _newDocumentId;

        #endregion    
  
        #region Constructors

        public VoBoAssignDocumentCommand(SPFile sourceFile, SPList targetLibrary)
        {
            _sourceFile = sourceFile;
            _targetLibrary = targetLibrary;
        }

        #endregion

        #region Command Methods

        public void Execute()
        {
            UTF8Encoding encoding = new System.Text.UTF8Encoding();
            byte[] content = encoding.GetBytes(string.Empty);
            int docsCount = this.getDocumentsPostFix(_sourceFile.Name, _targetLibrary);
            string newDocumentName = _sourceFile.Name + "-" + docsCount + 1;
            SPFile newItem = _targetLibrary.RootFolder.Files.Add(newDocumentName, content);
            _newDocumentId = newItem.Item.ID;
            newItem.Item[SPBuiltInFieldId.ContentType] = GlobalConstants.CTTN_VOBO;
            newItem.Item[SPBuiltInFieldId.Title] = _sourceFile.Name;
            newItem.Item[SPBuiltInFieldId.URL] = SPContext.Current.Web.Url + _sourceFile.Url;
            newItem.Item[CustomSiteColumns.FIELD_VOBO_ID] = _sourceFile.Item.ID;
            newItem.Item[CustomSiteColumns.FIELD_DOCSET_ID_RADICADO] = _sourceFile.Name;
            newItem.Item[CustomSiteColumns.FIELD_DOCSET_KIND] = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_TIPOS, GlobalConstants.METADATA_TERM_TIPO_VB);
            newItem.Item[CustomSiteColumns.FIELD_VOBO_DOC_OWNER] = new SPFieldUserValue { LookupId = SPContext.Current.Web.CurrentUser.ID };
            newItem.Item.Update();
        }

        public void Undo()
        {
            SPListItem newItem = _targetLibrary.GetItemById(_newDocumentId);
            newItem.Delete();
            _targetLibrary.Update();
        }

        #endregion

        #region private methods

        protected int getDocumentsPostFix(string documentName, SPList currentList)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format(@"<Where>
                              <And>
                                 <Eq>
                                    <FieldRef Name='FileLeafRef' />
                                    <Value Type='Text'>{0}</Value>
                                 </Eq>
                                 <Eq>
                                    <FieldRef Name='ContentType' />
                                    <Value Type='Text'>{1}</Value>
                                 </Eq>
                              </And>
                           </Where>", documentName, GlobalConstants.CTTN_VOBO);
            SPListItemCollection result = currentList.GetItems(query);
            return result.Count;
        }

        #endregion
    }
}
